package com.jdd.pedicure.controller;

import com.jdd.pedicure.entity.Result;
import com.jdd.pedicure.entity.Room;
import com.jdd.pedicure.service.RoomService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RequestMapping("room")
@Controller
public class RoomController {
    @Resource
    RoomService roomService;

    @RequestMapping("goRoom")
    public String goRoom(){
        return "room";
    }
    @RequestMapping("goAdd")
    public String goAdd(Integer id, HttpServletRequest request){
        Room room=new Room();
        if(id!=null){
            room=roomService.queryById(id);
        }
        request.setAttribute("room",room);
        return "roomAdd";
    }
    @RequestMapping("save")
    @ResponseBody
    public Result save(@RequestBody Room room){
        Result result=new Result();
        if(room.getRoomName().equals("")||room.getRoomPrice().equals("")||room.getRoomType().equals("")||room.getRoomState().equals("")){
            result.setCode("1");
            result.setMsg("更新房间失败！房间相关信息不能为空！");
        }else{
                roomService.saveOrUpdate(room);
                result.setCode("0");
                result.setMsg("更新房间成功！");
        }
        return result;
    }
    @RequestMapping("delete")
    @ResponseBody
    public Result delete(int id){
        Result result=new Result();
        Room room = roomService.queryById(id);
        if(room.getRoomState().equals("1")){
            result.setCode("1");
            result.setMsg("删除房间失败！该房间正在使用中！");
        }else {
            roomService.delete(id);
            result.setCode("0");
            result.setMsg("删除房间成功！");
        }
        return result;
    }
    @RequestMapping("getData")
    @ResponseBody
    public Result<Room> getData(Result<Room> roomResult,Room example){
        roomResult.setExample(example);
        roomService.queryByPage(roomResult);
        roomResult.setCode("0");
        roomResult.setMsg("查询成功");
        return roomResult;
    }

    @RequestMapping("getroomid")
    @ResponseBody
    public Result<Room> getroomid(){
        Result roomResult=new Result();
        Room room = new Room();
        room.setRoomState("0");
        roomResult.setExample(room);
        Result result = roomService.queryAllfree(roomResult);
        roomResult.setCode("0");
        return roomResult;
    }
}
